package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.task.ReceiverType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 群聊消息发送结果回调
 * <p>
 * 将群聊消息发送的结果回调给商家
 *
 * <pre>
 * {
 *     "Data": {
 *         "nIsHit": 1,
 *         "MsgData": [
 *             {
 *                 "nIsHit": 0,
 *                 "vcDesc": null,
 *                 "vcHref": null,
 *                 "nMsgNum": 1,
 *                 "vcTitle": null,
 *                 "nMsgType": 2001,
 *                 "AtInGroup": [],
 *                 "dtMsgTime": "2025-02-12 17:32:27.108",
 *                 "nEmojiLen": 0,
 *                 "msgContent": "57Sg5p2Q576k5o6o6YCB57Sg5p2Q576k5o6o6YCB57Sg5p2Q576k5o6o6YCB57Sg5p2Q576k5o6o6YCB57Sg5p2Q576k5o6o6YCB",
 *                 "nVoiceTime": 0,
 *                 "vcEmojiMd5": null
 *             }
 *         ],
 *         "nMsgNum": 1,
 *         "vcMsgId": "3379054844069725906",
 *         "vcToWxId": "",
 *         "vcChatRoomId": "58071591617@chatroom",
 *         "vcErrorMsgData": null,
 *         "vcRelaSerialNo": "202502121732255871805",
 *         "vcChatRoomSerialNo": "DE6B91F1E60277AE6D0A16EB11C04F56"
 *     },
 *     "nType": 5002,
 *     "nResult": 1,
 *     "vcResult": "消息发送成功",
 *     "nTimeStamp": 1739352747110,
 *     "vcSerialNo": "20250212173226350010529191955",
 *     "vcRobotWxId": "wxid_kultbellcta622",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "7155800509D08EAFBB39A2DCE845347E"
 * }
 * 或
 * {
 *     "Data": {
 *         "nIsHit": 0,
 *         "MsgData": [
 *             {
 *                 "vcDesc": null,
 *                 "vcHref": null,
 *                 "nMsgNum": 1,
 *                 "vcTitle": null,
 *                 "nMsgType": 2001,
 *                 "AtInGroup": [],
 *                 "dtMsgTime": "2025-02-19 11:38:54.384",
 *                 "nEmojiLen": 0,
 *                 "msgContent": "123",
 *                 "nVoiceTime": 0,
 *                 "vcEmojiMd5": null
 *             }
 *         ],
 *         "nMsgNum": 0,
 *         "vcMsgId": "",
 *         "vcToWxId": "",
 *         "vcErrorMsgData": [
 *             {
 *                 "Code": 5009,
 *                 "Message": "群不存在",
 *                 "nMsgNum": 1
 *             }
 *         ],
 *         "vcRelaSerialNo": "202502191138538788575",
 *         "vcChatRoomSerialNo": "67B953E0977FCAE9DE89FA0F10BC5CF6"
 *     },
 *     "nType": 5002,
 *     "nResult": 5009,
 *     "vcResult": "群不存在",
 *     "nTimeStamp": 1739936334384,
 *     "vcSerialNo": "20250219113854306715919954664",
 *     "vcRobotWxId": "wxid_bysnjh4y1f1922",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "DA1B048A0F72A1142FE1F29C982E3B79"
 * }
 * 或
 * {
 *     "Data": {
 *         "nIsHit": 0,
 *         "MsgData": [
 *             {
 *                 "vcDesc": null,
 *                 "vcHref": null,
 *                 "nMsgNum": 1,
 *                 "vcTitle": null,
 *                 "nMsgType": 2001,
 *                 "AtInGroup": [],
 *                 "dtMsgTime": "2025-02-25 15:52:55.323",
 *                 "nEmojiLen": 0,
 *                 "msgContent": "登录APP推送任务，拉小蜜蜂进小助测试群",
 *                 "nVoiceTime": 0,
 *                 "vcEmojiMd5": null
 *             }
 *         ],
 *         "nMsgNum": 0,
 *         "vcMsgId": "",
 *         "vcToWxId": "",
 *         "vcErrorMsgData": [
 *             {
 *                 "Code": 4502,
 *                 "Message": "机器人不在群内",
 *                 "nMsgNum": 1
 *             }
 *         ],
 *         "vcRelaSerialNo": "202502251552548375097",
 *         "vcChatRoomSerialNo": "DE74ABB2CA5BF1BC921F838DCB1B0E47"
 *     },
 *     "nType": 5002,
 *     "nResult": 4502,
 *     "vcResult": "机器人不在群内",
 *     "nTimeStamp": 1740469975323,
 *     "vcSerialNo": "20250225155255254024528334445",
 *     "vcRobotWxId": "wxid_bysnjh4y1f1922",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "DA1B048A0F72A1142FE1F29C982E3B79"
 * }
 * </pre>
 */
@Slf4j
@Component
public class _5002Handler extends SendChatMessagesHandler {

    @Resource
    private PushResultHandler pushResultHandler;

    @Override
    public String name() {
        return "群聊消息发送结果回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 5002;
    }

    @Override
    protected void handle0(String actionType, String actionId, Callback callback, JSONObject data) {
        if (actionType.equals("push")) {
            long taskId = Long.parseLong(actionId);
            pushResultHandler.handle(callback, taskId, ReceiverType.ROOM, data.getString("vcChatRoomId"));
        } else {
            log.error("未知的消息发送类型[id:{}, serialNo:{}, action:{}]", callback.getId(), callback.getSerialNo(), actionType);
        }
    }
}
